<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>前后端分离项目业务接口测试</title>
    <script type="text/javascript" src="../js/vue.js"></script>
    <script type="text/javascript" src="../js/axios.min.js"></script>
</head>
<body>
<div id="app">
    <div>
        {{ msg }}
        <button type="button" @click="toLogin">重新登录</button>
        <button type="button" @click="logout">退出登录</button>
    </div>
    <div>
        <table>
            <tr>
                <td>主键</td>
                <td>用户名</td>
                <td>密码</td>
                <td>生日</td>
                <td>年龄</td>
                <td>创建时间</td>
            </tr>
            <tr v-for="content in contents">
                <td>{{ content.id }}</td>
                <td>{{ content.uname }}</td>
                <td>{{ content.pword }}</td>
                <td>{{ content.birthday }}</td>
                <td>{{ content.age }}</td>
                <td>{{ content.createTime }}</td>
            </tr>
        </table>
    </div>
</div>

<script type="text/javascript">
    let a = new Vue({
        el: "#app",
        data() {
            return {
                msg: "",
                contents: []
            };
        },
        methods: {
            toLogin() {
                window.location.href = "login.html";
            },
            logout() {
                axios.post("http://localhost:8082/myhome/logout", {
                    test: 111
                }, {headers: {'X-Access-Token': window.localStorage.getItem("token")}}).then(res => {
                    if (res.success) {
                        alert(res.message);
                    }
                });
            },
            checkLogin() {
                axios.post("http://localhost:8082/myhome/checkLogin", {
                    aaa: 1
                }, {headers: {'X-Access-Token': window.localStorage.getItem("token")}}).then(res => {
                    if (res.data.success === false) {
                        // this.$router.push("/login.html");
                        window.location.href = "login.html";
                    }
                    console.log(res.data.success, res.data.message);
                }).catch((err) => {
                    console.error(err);
                });
            },
            localContent() {
                axios.post("http://localhost:8082/myhome/tuser/list4", {
                    aaa: 1
                }, {headers: {'X-Access-Token': window.localStorage.getItem("token")}}).then(res => {
                    if (res.data.success) {
                        this.contents = res.data.data;
                    }
                    this.msg = res.data.message;
                }).catch((err) => {
                    console.error(err);
                });
            }
        },
        /**
         * 先(加载数据时最好放在这里)
         */
        created() {
            this.checkLogin();
            this.localContent();
        },
        /**
         * 后(document渲染完成后)
         */
        mounted() {
        }
    });
</script>
</body>
</html>